package GUIs;

import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.ImageIcon;


public class LoadingUI extends JFrame {

private static final long serialVersionUID = 1L;
	
	//controls
	private JPanel contentPane;
	private JLabel lblWait = new JLabel("\u05D0\u05E0\u05D0 \u05D4\u05DE\u05EA\u05DF..");
	
	private long showTimeStamp;
	private final static long SHOW_MIN_TOLERANCE = 1000;	//ms

	/**
	 * Constructor.
	 */
	public LoadingUI() {
		
		//form
		setAlwaysOnTop(true);
		setBackground(new Color(238, 232, 170));
		setTitle("Cinema");
		setIconImage(Toolkit.getDefaultToolkit().getImage(ClientUI.class.getResource("/resources/movie32.png")));
		setResizable(false);	
		setBounds(100, 100, 270, 131);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setUndecorated(true);
		
		//lblCaption
		JLabel lblCaption = new JLabel("\u05DE\u05D0\u05EA\u05D7\u05DC \u05D0\u05EA Cinema");
		lblCaption.setHorizontalAlignment(SwingConstants.CENTER);
		lblCaption.setForeground(new Color(0, 0, 0));
		lblCaption.setFont(new Font("Arial", Font.PLAIN, 18));
		lblCaption.setBounds(23, 22, 213, 42);
		contentPane.add(lblCaption);
		
		//lblWait
		lblWait.setHorizontalAlignment(SwingConstants.RIGHT);
		lblWait.setForeground(new Color(0, 0, 255));
		lblWait.setFont(new Font("Arial", Font.PLAIN, 12));
		lblWait.setBounds(54, 75, 99, 17);
		contentPane.add(lblWait);
		
		//lblBackground
		JLabel lblBackground = new JLabel("");
		lblBackground.setIcon(new ImageIcon(LoadingUI.class.getResource("/resources/bg_loading.png")));
		lblBackground.setBounds(0, 0, 270, 131);
		contentPane.add(lblBackground);
				
	}
	
	
	/* ============================ *
	 * ===Provided functionality=== *
	 * ============================ */
	
	/**
	 * show loading screen to user.
	 */
	public void showLoadingScreen(){
		showTimeStamp = System.currentTimeMillis();
		setVisible(true);
	}
	
	/**
	 * hide the form from user, after was visible for at least 'SHOW_MIN_TOLERANCE' ms.
	 */
	public void hideLoadingScreen(){
		while (System.currentTimeMillis()-showTimeStamp < SHOW_MIN_TOLERANCE){}
		setVisible(false);
		dispose();
	}
}
